package com.joysuch.wwyt.core.common.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MhAesUtil {
	private static final Logger logger = LoggerFactory.getLogger(MhAesUtil.class);
	public static final String CHARTSET = "utf-8";
	private static final String KEY_ALGORITHM = "AES";
	private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";// 默认的加密算法

	/**
	 * AES 加密操作
	 *
	 * @param content 待加密内容
	 * @param key     加密密码
	 * @param iv      使用CBC模式，需要一个向量iv，可增加加密算法的强度
	 * @return 加密数据
	 */
	public static String encrypt(String content, String key, String iv) {
		try {
			// 创建密码器
			Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);

			// 密码key(超过16字节即128bit的key，需要替换jre中的local_policy.jar和US_export_policy.jar，否则报错：Illegal
			// key size)
			SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARTSET), KEY_ALGORITHM);

			// 向量iv
			IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(CHARTSET));

			// 初始化为加密模式的密码器
			cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec);

			// 加密
			byte[] byteContent = content.getBytes(CHARTSET);
			byte[] result = cipher.doFinal(byteContent);

			return Base64.encodeBase64String(result);
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		}

		return null;
	}

	/**
	 * AES 解密操作
	 *
	 * @param content 密文
	 * @param key     密码
	 * @param iv      使用CBC模式，需要一个向量iv，可增加加密算法的强度
	 * @return 明文
	 */
	public static String decrypt(String content, String key, String iv) {

		try {
			// 创建密码器
			Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);

			// 密码key
			SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARTSET), KEY_ALGORITHM);

			// 向量iv
			IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(CHARTSET));

			// 初始化为解密模式的密码器
			cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);

			// 执行操作
			byte[] result = cipher.doFinal(Base64.decodeBase64(content.getBytes(CHARTSET)));

			return new String(result, CHARTSET);
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		}

		return null;
	}

	public static void main(String[] args) {
		String result = MhAesUtil.decrypt("vuCIW1gTe70lQErt46Raer9WCU9GwrYJuKJV1/L1+/crQr/DEWRN7kN3zsRmcGsRPFg5Ap3EbbYkbVRYJgj9UMdqsFS/JPtOPFwY0EHHCq4fFoOcfgROPYcnafHBzO1BFqjfE14hlRJO7C7c7Dtig7flJfSR5eYacug2tvHUlk4/H/xQSH/tI2LTGfPrmvPpbmy/DO50wumOX7irgQRQRuY5zLRHjArrM0vlfNxPSM2OV+UvZUSYdRsT+94vmDUmROnTbmEyMULPqYNdmGYJyoYDr0LdRSvoKsVqiaONQLhSo1huKVR90LZa+y89vIXsskdsETbDJ+7RN7g8cSqGBOqG6oYnKt3fAa6Y17F0+X6eTHT6JviXFaZYobNFviAejO7eHSoHx0ZFPnZJIEkS6lcp3hxEHqNMAiZu2l8BGXsgjNKVS8E0PRGz326yebfQs1Cp7uPvtTCoe1WzI3OG21WWNg8nuI43LlguyIiS0j41hz+f+wLuiN6OCWj35UDY0OISvjFW5UPeuv8YJ1qXe1Wy4dFn2jz6Yru/KQGSPHPJ4ntDQWLT0PzgprOB1eCcE2ldTt5B+GT47ofIe+l08Axdyxqqg5pgUFhNvTIthjdAJ5iFVIjh5OqbNP/3FWI5AS7G5byVCBuu8ApfS9L6FC2ODIiqRfFx8A1CVR7QyMwRR54qSl7ls9d3RGkOnIV2qMvQelFT36ihkn3/M9DiD1NLf/yxenlRYrL5NohK9Hxa24TiaixPqTYdnFX3RBiF/a/ZNy/zQLn1gTz3p7cPd9du/Q+HTE7hjs+nfPROkkHprr1ZY2LczvKAA5V69/K3RLQowbiO095dteBpToE3lqUh9T3lmnwxjbySMO/pKLC3SJZuDzcKGd6AgyAWQy6ju1QxdbnSTp5G1SegjHynTiZkDqrloCGdwcjO7fL0jciacs/YflXZN+HTBxHzPcJu",
				"gJHbfYsRDzYBOmbd", "nGgpwcmCkXSjmeSz");
		System.out.println(result);
		System.out.println(MhAesUtil.encrypt("{\"collectTime\":\"20200623164304\",\"dataId\":\"5ef1c01cd055702728821d5f\",\"dataType\":\"energy_data\",\"datas\":[{\"quotaId\":\"320510122002K0001Q1011\",\"value\":0},{\"quotaId\":\"320510122001G0001WD001\",\"value\":28.536},{\"quotaId\":\"320510122002K0001Q1007\",\"value\":0},{\"quotaId\":\"320510122001G0003WD001\",\"value\":28.235},{\"quotaId\":\"320510122002K0001Q1004\",\"value\":0},{\"quotaId\":\"320510122001G0001Q1001\t\",\"value\":2},{\"quotaId\":\"320510122002K0001Q1003\",\"value\":0},{\"quotaId\":\"320510122002K0001Q1002\",\"value\":0}],\"enterpriseId\":\"320510122\",\"gatewayId\":\"32051012201\",\"isConnectDataSource\":true,\"reportType\":\"report\"}",
				"gJHbfYsRDzYBOmbd", "nGgpwcmCkXSjmeSz"));
		// {"enterpriseId":"320510122","report":"LrG8Dy3loip3YOYusHH0c4GflmoLhdhZC2xIyZ5SJa+3vS72OwmSiKX0PxdwJj4moThQkde04e8vs5TayJzRIyElfGeE6iKDb/Cott00XBymSq0WZUI/WnhHp8h970otFUDXbCOCpAhNoBue9c6FchAqFrptgI7lOmXoiUAOTdiY3oGdfA+biLxi4xKzLR8yrbu3M5LSe/LPz0zMXGzq6cmdert9VJcJD02KPDvj9QF3mbtNzGiD1sqAC+sNNUJeVb6W8ol/HV6UZ7jdV6hsB97KzARUsDEi45EDL3TWqmxQyJsx/S0KRn7C8FbGeWI0ah6Wx7c0W7Wrm6n7K1Un4UXxo1B0ZjDz2dmcCa/l56Ck/CNIf73taQ9QGKD0ArPmFFU8U4iM85/y6rRZ9wkxTAtS5tzejlv3bZUUfPEVJ5zWw2HTwViPstLoNuBwHj+2J/39m65dxuS3f5dfHXxzC+1KDNW4fyqrjlfVGUt/MUd2nPMPusafHKXZIxOWFCOW70WhoJzePjNpFnoyWc5u083J0ixBAOdxH31w/RUrN1coF/85CoXlOI8/xxefTzt+0Js2s7GDPQs2D1KJFcRK0BfDRiweeAt3cv79LjtFZFBASAhZK/PAjCOfB/b0raFMjowkrlaLOCNDPPCtqbsU2RxNmBgW9cWfj+xasF2t5Q5uupAwUvv14ROriecHnb4UyDSCS3ZbRUg4DZ8SkewWAofH57F5rO9VrAZbbDBQF8J4Dkq8B4n05eQN7yd3MlM11QP68nOcJyAeGV+rHyaXFmcb3hS7Nu3Z+zOMYEUoNh8="}
		
	}
	
}
